#coding:utf8
from config import *
import multiprocessing as mp  
from routin import *
qq = None

def handle_event(e): #: winrawin.RawInputEvent
    if str(e.name)=='thumb1' and str(e.code)=='4':
        if str(e.event_type)=='up':
            qq.put(( 't1up',0,0,0,0))
        else:
            qq.put(( 't1down',0,0,0,0))
    if str(e.name)=='thumb2' and str(e.code)=='5':
        if str(e.event_type)=='up':
            qq.put(( 't2up',0,0,0,0))
        else:
            qq.put(( 't2down',0,0,0,0))

    if e.event_type == 'move':
        qq.put(( 'm', e.device.handle, e.delta_x, e.delta_y, time.time()))
    if e.event_type == 'wheel-up':
        qq.put(( 'w', e.device.handle, int(e.code), e.delta_y, time.time()))
    if e.event_type == 'wheel-down':
        qq.put(( 'w', e.device.handle, -int(e.code), e.delta_y, time.time())) 
    
      
def foo(q):
    global qq
    qq = q
    import winrawin
    import tkinter as tk    
    window = tk.Tk()
    window.maxsize(50, 50)
    winrawin.hook_raw_input_for_window(window.winfo_id(), handle_event)
    window.mainloop()

def aa2(a):    
    x1, y1, x2, y2 = a
    xx = x1 + x2
    yy = y1 + y2
    d3 = math.sqrt(xx * xx + yy * yy)
    d1 = math.sqrt(x1 * x1 + y1 * y1)
    d2 = math.sqrt(x2 * x2 + y2 * y2)
    du = math.atan2(y1, x1)
    du2 = math.atan2(y2, x2)
    d = du2 - du
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi   
    
    if d3 > max(d2 , d1):
        return
    
    if d1 < 1 or d2 < 1:
        return
        
    if abs(d) < math.pi * 0.5:
        return
    
    a = -1
    
    if abs(x2) > abs(y2):
        if x2 > 0:
            a = 1
    else:
        if y2 > 0:
            a = 1    
    return a * max(d1, d2)

import queue
gq = queue.Queue()

def one_loop():
    dx1 = 0
    dy1 = 0
    dx2 =0
    dy2 = 0    
    while not gq.empty():               
        a = gq.get()        
        h, x, y, t = a
        if h in cfg.left_mouse:
            dx1 += x
            dy1 += y
        else:
            dx2 += x
            dy2 += y
    return dx1, dy1, dx2, dy2

def add_mouse(h):
    if len(cfg.left_mouse) == 0:
        cfg.left_mouse.append(h)
        print('add mouse', cfg.left_mouse, cfg.right_mouse)
    if h in cfg.left_mouse:
        return
    if h in cfg.right_mouse:
        return
    cfg.right_mouse.append(h)
    print('add mouse', cfg.left_mouse, cfg.right_mouse)


def do_mouse_jump(h):    
    add_mouse(h)
    if h in cfg.left_mouse:
        cfg.left_mouse_time = time.time()
    if h in cfg.right_mouse:
        cfg.right_mouse_time = time.time()
        
def deal_x2_move(x, y):
    dx = cfg.snap[1][1] - cfg.snap[0][1] 
    dy = cfg.snap[1][2] - cfg.snap[0][2]
    du = math.atan2(dy, dx)
    du2 = math.atan2(y, x)
    d = du2 - du
    if d >  math.pi:
        d -= 2 * math.pi
    if d < - math.pi:
        d += 2 * math.pi
    dd = math.cos(d) *math.sqrt(x * x + y * y) *get_x2_d_sca()
    cfg.x2_mode_d += dd
    cfg.x2_mode_d = min(100, cfg.x2_mode_d)
    cfg.x2_mode_d = max(0, cfg.x2_mode_d)
    
    #print('dd', cfg.x2_mode_d)
    x2_d_to_dst()
        

def deal_invert():
    if not cfg.has_invert:        
        if cfg.to_change_time > 0:
            print('inv 1', cfg.to, cfg.to_change_time)
            return False                        
        cfg.has_invert = True
        return True        
    return True    
    
con_sec_move = 0
last_hand = 0

def con_set(a, hasTo):
    global  con_sec_move, last_hand
    d1, d2, d3, d4 = a
    zero_move =  (d1 == 0 and d2 == 0 and d3 ==0 and d4 == 0)
    left_m = (d1 !=0 or d2 != 0)
    right_m = (d3 !=0 or  d4 != 0)
    
    if cfg.start_show_time == 0:
        con_sec_move = 0
        return
    if zero_move:
        return
    
    #if con_sec_move < 5 and not cfg.has_pos_vert:
        #print(left_m, right_m, hasTo, con_sec_move)
    if hasTo:
        con_sec_move = 0
        return
    
   
    
    if left_m and right_m:
        con_sec_move = 0
        return
    
    if con_sec_move == 0 and not left_m:
        con_sec_move += 1        
        return
    
    if left_m:
        con_sec_move = 0
        return
    
    con_sec_move += 1
    if con_sec_move >= 4:
        if not cfg.has_pos_vert:
            print('con', con_sec_move)
        cfg.has_pos_vert = True        
    
    
def thrf():        
    while True:
        time.sleep(0.05)
        a = one_loop()                 
        dd = aa2(a)
        con_set(a, dd != None)      
         
        if dd == None:
            continue
        
        if not cfg.w2showing:
            continue
        
        if dd > 0:
            cfg.pos_vert_len += dd            
            if cfg.pos_vert_len > 10:
                if not cfg.has_pos_vert:
                    print('pos len', cfg.pos_vert_len)
                cfg.has_pos_vert = True
        else:
            cfg.pos_vert_len -=1
            
        if dd < 0 and not cfg.has_pos_vert:
            if enable_invert and deal_invert():
                dd = -dd
            else:
                if not cfg.has_pos_vert:
                    print('inv 3')
                cfg.has_pos_vert = True
                    
        if (cfg.to >= max_l and dd > 0) or  (cfg.to <= 0 and dd < 0):
            cfg.to_time = time.time()
            continue        
        
        to_lock.acquire()        
        cfg.to = cut_to(cfg.to+dd*get_to_sca())
        cfg.to_time = time.time()
        set_to_change_time()
        to_lock.release()        
        #print('to',cfg.to )
    